<?php
/**
 * 模式设置
 * User: zal
 * Date: 2016/2/27
 * Time: 16:39
 */
namespace Api\Controller;
use Api\Controller\BaseController;

class UserModeController extends BaseController {

    //下单类型
    const ORDER_FORTHWITH         =   1;      // 即时下单
    const ORDER_APPOINT       =   0;      // 预约下单
    //计费方式
    const CHARGING_FOLLOWING =1;    //顺风
    const CHARGING_CRITERION =2;   //标准
    const CHARGING_MAJOR =3;        //专修
    //上门方式
    const TRAFFIC_BUS =1;    //公交车
    const TRAFFIC_WALK =2;   //步行
    const TRAFFIC_DRIVE =3; //自驾车

    public function index(){
        $data=$modeArray=array();
        $userMode=M('UserMode');
        $user_id=I('get.user_id','');
        //$cate=I('get.cate','');
        $userData=$this->checkUserIsExist($user_id);
        if(isset($userData["code"])){
            $this->ajaxReturn($userData,'JSON');
        }

        $userModes=$userMode->where("user_id=".$user_id)->select();
        if(!empty($userModes)) {
            foreach ($userModes as $val) {
                $modeArray["modes"][$val["mode_cate"]]["cate"] = $val["mode_cate"];
                $modeArray["modes"][$val["mode_cate"]]["order"] = $val["mode_order"];
                $modeArray["modes"][$val["mode_cate"]]["charging"] = $val["mode_charging"];
            }
            $modeArray["user_id"]=$userModes[0]["user_id"];
            $modeArray["traffic"]=$userModes[0]["mode_traffic"];
            $data["data"]=$modeArray;
            $data["code"]=200;
            $data["msg"]='ok';
        }else{
            $data["code"]=200;
            $data["msg"]='还没有设置模式设置';
        }
        $this->ajaxReturn($data,'JSON');
    }

    /**
     * 模式设置
     */
    public function set(){
        $data=$sql=$insetData=array();
        $userMode=M('UserMode');
        $user_id=I('post.user_id','');
        $cates=I('post.cate','');
        $orders=I('post.order','');
        $chargings=I('post.charging','');
        $traffic=I('post.traffic','');
        $userData=$this->checkUserIsExist($user_id);
        if(isset($userData["code"])){
            $this->ajaxReturn($userData,'JSON');
        }
        $result=false;
        if(empty($cates)||empty($orders)||empty($chargings)){
            $data["code"]=1001;
            $data["msg"]='缺少参数';
        }else{
            if(!is_array($cates)||!is_array($orders)||!is_array($chargings)){
                $data["code"]=1002;
                $data["msg"]='参数必须是数组';
            }else{
                foreach($cates as $k=>$val){
                    $cate=$val;
                    $order=$orders[$k];
                    $charging="";
                    if(isset($chargings[$k])){
                        $charging=$chargings[$k];
                    }
                    $insetData['user_id']=$user_id;
                    $insetData['mode_cate']=$cate;
                    $insetData['mode_order']=$order;
                    $insetData['mode_charging']=$charging;
                    $insetData['mode_traffic']=1;
                    $userModes=$userMode->where("user_id=".$user_id." and mode_cate=".$cate)->find();
                    if(empty($userModes)){
                        $result=$userMode->add($insetData);
                    }else{
                        $result=$userMode->where('umid='.$userModes["umid"])->save($insetData);
                    }
                }
                //$sql['user_id']=$user_id;
                //$sql['modes']=serialize($insetData);
                //$sql['mode_traffic']=$traffic;
                if($result===false){
                    $data["code"]=403;
                    $data["msg"]='操作失败';
                }else{
                   if($cate==1){
                       $c=session('repair_config');
                       $repairWhere['user_id']=$user_id;
                       $repairWhere['mode_cate']=1;
                       $m=M('user_mode')->field('mode_order,mode_charging')->where($repairWhere)->select();

                     //  var_dump(session('repair_config'));exit;
                       session('mode',$m);
                    //   var_dump(session('repair_config'));exit;
                     //  var_dump( $c['mode']);exit;
                   }

                    $data["code"]=200;
                    $data["msg"]='ok';
                }
            }
        }
        $this->ajaxReturn($data,'JSON');
    }


}